<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String id = request.getParameter("id");
if (id == null) {
	id = "";
}
%>
<script type="text/javascript">
$.fn.tree.defaults.loadFilter = function(data, parent) {
	var opt = $(this).data().tree.options;
	var idFiled, textFiled, parentField;
	if (opt.parentField) {
		idFiled = opt.idFiled || 'id';
		textFiled = opt.textFiled || 'text';
		parentField = opt.parentField;
		var i, l, treeData = [], tmpMap = [];
		for (i = 0, l = data.length; i < l; i++) {
			tmpMap[data[i][idFiled]] = data[i];
		}
		for (i = 0, l = data.length; i < l; i++) {
			if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
				if (!tmpMap[data[i][parentField]]['children'])
					tmpMap[data[i][parentField]]['children'] = [];
				data[i]['text'] = data[i][textFiled];
				tmpMap[data[i][parentField]]['children'].push(data[i]);
			} else {
				data[i]['text'] = data[i][textFiled];
				treeData.push(data[i]);
			}
		}
		return treeData;
	}
	return data;
};
	$(function() {
		$('#grantTree').tree({
			url : '${pageContext.request.contextPath}/menuAction!getAllTree.action',
			parentField : 'pid',
			checkbox : true,
			formatter : function(node) {
				return node.text;
			},
			onLoadSuccess : function(node, data) {
				$.post('${pageContext.request.contextPath}/menuAction!getResourceTree.action', {
					id : $(':input[name="data.id"]').val()
				}, function(result) {
					if (result) {
						for (var i = 0; i < result.length; i++) {
							var node = $('#grantTree').tree('find', result[i].id);
							if (node) {
								var isLeaf = $('#grantTree').tree('isLeaf', node.target);
								if (isLeaf) {
									$('#grantTree').tree('check', node.target);
								}
							}
						}
					}
				}, 'json');
			}
		});
	});
</script>
	<input name="data.id" value="<%=id%>" readonly="readonly" type="hidden" />
		<ul id="grantTree"></ul>